package com.wfg.common;

import com.wfg.common.msg.Invocation;
import org.apache.commons.io.IOUtils;

import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author wufagang
 * @description
 * @date 2023年05月11日 08:31
 */
public class HttpClient {

    public String send(String hostname, Integer port, Invocation invocation) {
        try {
            URL url = new URL("http", hostname, port, "/");
            HttpURLConnection httpuRLConnection = (HttpURLConnection) url.openConnection();
            httpuRLConnection.setRequestMethod("POST");
            httpuRLConnection.setDoOutput(true);
            OutputStream outputStream = httpuRLConnection.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(outputStream);
            oos.writeObject(invocation);
            oos.flush();
            oos.close();
            InputStream inputStream = httpuRLConnection.getInputStream();
            return IOUtils.toString(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
